package statistics.tests;

import statistics.Asonance.IAsonance;
import statistics.Pocitani;

import java.util.ArrayList;

/*
*   File Scheffe 
*   in package statistics.tests
*   was created by slaha
*   on 2.4.12 at 22:07.
*
*/
public class Scheffe {
  double[][] porovnani;
  ArrayList<Souradnice> souradnice = new ArrayList<>();
  IAsonance asonance;

  public Scheffe(ITest itest, IAsonance asonance) {

    this.asonance = asonance;

    if (itest instanceof Anova) {
      vypocetAnova((Anova) itest);
    } else if (itest instanceof KruskallWallis) {
      vypocetKW((KruskallWallis) itest);
    }
  }
  public Scheffe(Anova anova) {
    vypocetAnova(anova);
    }

  public Scheffe(KruskallWallis kw) {
    vypocetKW(kw);
  }

  private void vypocetKW(KruskallWallis kw) {
    porovnani = new double[kw.getData().size() - 1][kw.getData().size()];
    for (int i = 0; i < porovnani.length; i++) {
      for (int j = i + 1; j < porovnani[i].length; j++) {
        try {
          double prumerI = kw.getPrumer(i);
          double prumerJ = kw.getPrumer(j);


          //double chi = new Pocitani().getChiKritickaHodnota(kw.getKvel()-1, 0.95);
          double chi = 7.235d;

          double zavorkaNi = ((1d / kw.getNi(i)) + (1d / kw.getNi(j)));
          double d = (kw.getNvel() * (kw.getNvel() + 1));
          d /= 12d;
          d *= zavorkaNi;
          d *= chi;
          d = Math.sqrt(d);

          porovnani[i][j] = Math.abs(prumerI - prumerJ);

          if (porovnani[i][j] > d) {
            souradnice.add(new Souradnice(i, j));
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  private void vypocetAnova(Anova anova) {
    porovnani = new double[anova.getKvel()][anova.getKvel()];
    for (int i = 0; i < porovnani.length; i++) {
      for (int j = i + 1; j < porovnani[i].length; j++) {
        try {
          ArrayList<Double> ali = anova.getHodnoty(i);
          ArrayList<Double> alj = anova.getHodnoty(j);
          double prumerI = anova.getPrumer(i);
          double prumerJ = anova.getPrumer(j);

          double d = ((1d / ali.size()) + (1d / alj.size()));
          double se = anova.getSe() / (anova.getNvel() - anova.getKvel());
          d *= se;
          d *= (anova.getKvel() - 1);
          d *= new Pocitani().getFKritickaHodnota(anova.getKvel() - 1, anova.getNvel() - anova.getKvel(), 0.95);
          d = Math.sqrt(d);

          porovnani[i][j] = Math.abs(prumerI - prumerJ);

          if (porovnani[i][j] > d) {
            souradnice.add(new Souradnice(i, j));
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
  }


  public String vypis() {
    String ret = "<table><tr><th>&nbsp;</th>";

    int pocetMereni = asonance.getaTest().getMereni().size();


    //..hlavička
    for (int i = 1; i < pocetMereni; i++) {
      ret += "<th>" + asonance.getaTest().getMereni().get(i).getJmeno() + "</th>";
    }
    ret += "</tr>";

    for (int i = 0; i < pocetMereni-1; i++) {
      //int sloupec = i+1;
      ret += "<tr>";

      ret += "<td><b>" + asonance.getaTest().getMereni().get(i).getJmeno() + "</b></td>";

      for (int nic = 0; nic < i; nic++) {
        ret += "<td>&nbsp;</td>";
      }
      for (int j = i+1; j < pocetMereni; j++) {

        ret += "<td>";

        String str = Double.toString((Math.round(porovnani[i][j]*1000)) / 1000d);
        ret += str;
        
        for (Souradnice s : souradnice) {
          if (s.x == i && s.y == j) {
            ret += " *";
            break;
          }
        }
        ret += "</td>";
        //sloupec++;
      }

      ret += "</tr>";
    }

    ret += "</table><br><br>";

    ret += "<h3>Lišící se básníci</h3><ul>";
    for(Souradnice s : this.souradnice) {
      String i = asonance.getaTest().getMereni().get(s.x).getJmeno();
      String j = asonance.getaTest().getMereni().get(s.y).getJmeno();
      ret += "<li>" + i + " a " + j;
    }
    ret += "</ul>";

  return ret;
  }

  class Souradnice {
    int x, y;

    Souradnice(int x, int y) {
      this.x = x;
      this.y = y;
    }
  }
}

